package com.outside.landingpage.util;



import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class IpAddressUtils {
        /**
         * 空字符串
         */
        private static final String NULLSTR = "";

        public static String getIpAddress(HttpServletRequest request) {

            String ip = request.getHeader("X-Forwarded-For");
            if (isNotEmpty(ip) && StringUtils.indexOf(ip, ',') != -1) {
                String[] ips = StringUtils.split(ip, ',');
                ip = ips[0];
                if (ip.equalsIgnoreCase("unknown") && ips.length > 1) {
                    ip = ips[1];
                }
            }
            if (isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                // WebLogic Plug-In Enabled
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("X-Real-IP");
            }
            // 代理没有设置，用request的
            if (isEmpty(ip) || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;
        }
        /**
         * * 判断一个字符串是否为空串
         *
         * @param str String
         * @return true：为空 false：非空
         */
        public static boolean isEmpty(String str) {
            return isNull(str) || NULLSTR.equals(str.trim());
        }

        /**
         * * 判断一个字符串是否为非空串
         *
         * @param str String
         * @return true：非空串 false：空串
         */
        public static boolean isNotEmpty(String str) {
            return !isEmpty(str);
        }

        /**
         * * 判断一个对象是否为空
         *
         * @param object Object
         * @return true：为空 false：非空
         */
        public static boolean isNull(Object object) {
            return object == null;
        }


}
